﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using MapEditor;

namespace PFAGame
{
    public enum Decor { LABO = 0, FOREST = 1, FACTORY = 2, DESERT = 3, _SIZE_ = 4 };

    /// <summary>
    /// surcouche du content manager pour gérer les changement de décor/type et pour convertir les id en texture.
    /// </summary>
    class AssetsMapManager
    {
        string basePath = "Images/Themes/";
        string extension = ".png";
        string[] decorPath = new string[] 
        { 
            "Labo/",
            "Forest/",
            "Factory/",
            "Desert/"

            
            
        };

        string[] tileNames = new string[]
        {
            "Terrain/ground_1",
            "Terrain/road_1",
            "Decors/decor_d_1",
            "Terrain/cliff_north",
            "Decors/decor_d_2",
            "Terrain/cliff_north_east",
            "Terrain/cliff_north_west",
            "Terrain/cliff_south",
            "Terrain/cliff_south_east",
            "Terrain/cliff_south_west",
            "Terrain/cliff_west",
            "Terrain/road_2",
            "Terrain/road_3",
            "Terrain/road_4",
            "Terrain/road_5",
            "Terrain/water",
            "Terrain/bridge_horizontal",
            "Terrain/cliff_east",
            "Decors/decor_e_1",
            "Decors/decor_f_1",
            "Decors/decor_f_2",
            "Decors/decor_g_1",
            "Decors/decor_g_2",
            "Decors/decor_a_2",
            "Decors/decor_a_5",
            "Decors/decor_a_6",
            "Decors/decor_b_2",
            "Decors/decor_b_3",
            "Decors/decor_b_3_alt",
            "Decors/decor_a_1",
            "Decors/decor_a_3",
            "Decors/decor_a_4",          
            "Decors/decor_b_1",
            "Decors/decor_b_4",
            "Decors/decor_b_4_alt",
            "Decors/decor_e_2",
            "Decors/decor_f_3",
            "Decors/decor_f_4",
            "Terrain/cliff_corner_1",
            "Terrain/cliff_corner_2",
            "Side/cube",
            "Side/ground_1",
            "Side/ground_2"
        };

        Dictionary<int, Texture2D> currentDecorTextures = new Dictionary<int, Texture2D>();
        Decor currentDecor = (Decor)(-1);

        public Decor CurrentDecor
        {
            get { return currentDecor; }
        }

        int currentType = -1;

        /// <summary>
        /// Charge un nouveau decor pour la map, s'il y a un changement.
        /// </summary>
        /// <param name="decor">nouveau decor</param>
        /// <param name="type">nouveau type de map (1 iso, 0 platform)</param>
        public void loadDecor(Decor decor, int type)
        {
            if (decor == currentDecor && type == currentType)
                return;
            currentDecorTextures = new Dictionary<int, Texture2D>();

            //TODO: chargement des textures correspondant au décor et au type voulu puis ajout dans le dictionaire
            currentDecorTextures.Clear();
            for (int i = 0; i < tileNames.GetLength(0); i++)
            {
                Texture2D tex = ScreenManager.Singleton.Game.Content.Load<Texture2D>(basePath + decorPath[(int)decor] + tileNames[i]);
                currentDecorTextures.Add(i, tex);
            }
        }

        /// <summary>
        /// Retourne une texture à partir de son id, si elle existe.
        /// </summary>
        /// <param name="id">id de la texture recherché</param>
        /// <returns>texture recherché, si elle est trouvé</returns>
        public Texture2D getTextureFromId(int id)
        {
            Texture2D output;
            if (currentDecorTextures.TryGetValue(id, out output))
                return output;
            return null;
        }

        #region singleton

        public AssetsMapManager()
        {
            Singleton = this;
        }

        public static AssetsMapManager Singleton
        {
            get 
            { 
                if (singleton != null)
                    return singleton;
                return new AssetsMapManager();
            }
            set { singleton = value; }
        }
        static AssetsMapManager singleton;

        #endregion
    }
}
